/* ========================================================================= */
/* ------------------------------------------------------------------------- */
/*!
  \file			formatmanager.h
  \date			Sep 2012
  \author		TNick

  \brief		Contains the definition for FormatManager class


*//*


 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 Please read COPYING and README files in root folder
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
/* ------------------------------------------------------------------------- */
/* ========================================================================= */
#ifndef __FORMATMANAGER_INC__
#define __FORMATMANAGER_INC__
//
//
//
//
/*  INCLUDES    ------------------------------------------------------------ */

#include	<QList>

/*  INCLUDES    ============================================================ */
//
//
//
//
/*  CLASS    --------------------------------------------------------------- */

#include	<cpg/cpg.h>
#include	<memtrack.h>

namespace	BBlocks		{

class	ByteBuffer;

}


namespace	cpg		{

class	IDrawFile;
//class	XmlDrawFile;
//class	SqlDrawFile;
//class	BinDrawFile;

/**
*	@brief	Manages the file formats that application knows about
*/
enum	FileFormatVersion		{
	DefaultVersion = 0
};


/**
*	@brief	Manages the file formats that application knows about
*
*	\ref file_formats "File formats page" contains an overview of the
*	mechanism used to deal with files.
*/
class CPGSHARED_EXPORT FormatManager			: public MemTrack		{
	BBM_TRACK( FormatManager );

	//
	//
	//
	//
	/*  DEFINITIONS    ----------------------------------------------------- */

	friend class IDrawFile;

#	define	PROTECTED_IDRAWFILE_CONTENT		0

	/*  DEFINITIONS    ===================================================== */
	//
	//
	//
	//
	/*  DATA    ------------------------------------------------------------ */

private:


	/**
	*	@brief	list of interfaces that we know of
	*/
	QList<IDrawFile*>			lst_;


	/**
	*	@brief	the default instance (default format)
	*/
	IDrawFile *					def_;


	/**
	*	@brief	singleton
	*/
	static FormatManager *		uniq_;


	/*  DATA    ============================================================ */
	//
	//
	//
	//
	/*  FUNCTIONS    ------------------------------------------------------- */

private:


	/**
	*	@brief	constructor;
	*/
	FormatManager				( void );


	/**
	*	@brief	destructor;
	*/
	~FormatManager				( void );



public:


	/**
	*	@brief	initialisation method
	*/
	static bool					init					( void );


	/**
	*	@brief	termination method
	*/
	static void					end						( void );


	/**
	*	@brief	termination method
	*/
	static inline FormatManager *	unique					( void )
	{ return uniq_; }



	/**
	*	@brief	get a list of extensions as required by
	*	QFileDialog::getOpenFileName()
	*/
	static QString				gofnExt				( void );


	/**
	*	@brief	see what instance to use (NULL if none fits) from file name
	*/
	static IDrawFile *			instFromFile		(
			const QString &				s_file_name
			);


	/**
	*	@brief	see what instance to use (NULL if none fits) from file content
	*/
	static IDrawFile *			instFromContent		(
			BBlocks::ByteBuffer &		p_data
			);


	/**
	*	@brief	find an instance that has this name
	*/
	static IDrawFile *			instFromName		(
			const QString &				s_name
			);


	/**
	*	@brief	the default instance
	*/
	static IDrawFile *			defaultInst			( void )
	{ return uniq_->def_; }


	/**
	*	@brief	change default instance
	*/
	static void					setDefaultInst		( IDrawFile * new_val );


//	/**
//	*	@brief	the build-in instance for binary files
//	*/
//	static BinDrawFile *		bldInBinary			( void );


//	/**
//	*	@brief	the build-in instance for xml files
//	*/
//	static XmlDrawFile *		bldInXML			( void );


//	/**
//	*	@brief	the build-in instance for sql files
//	*/
//	static SqlDrawFile *		bldInSQL			( void );




	/* ******************************************************************* */
	/** @name 	Methods regarding the list of IDrawFile instances          */
	/* ******************************************************************* */
	///@{


public:


	/**
	*	@brief	get the IDrawFile instance at a particular index or NULL if out of bounds
	*/
	static inline IDrawFile *	item			( int i )
	{
		if ( ( i < 0 ) || ( i >= uniq_->lst_.count()) )
			return NULL;
		return uniq_->lst_.at( i );
	}


	/**
	*	@brief	get the index of a IDrawFile instance or -1 if not found
	*/
	static inline int			indexOf			( IDrawFile * item )
	{ return uniq_->lst_.indexOf( item ); }


	/**
	*	@brief	number of IDrawFile instances that we store
	*/
	static inline int			count			( void )
	{ return uniq_->lst_.length(); }


	/**
	*	@brief	get'em all as a QList
	*/
	static inline QList<IDrawFile *>	list		( void )
	{ return uniq_->lst_; }


	/**
	*	@brief	tell if a particular IDrawFile is part of the IDrawFile instance list
	*/
	static inline bool			contains		( IDrawFile * it )
	{ return uniq_->lst_.contains( it ); }


	/**
	*	@brief	remove an item from the list
	*
	*	Will not remove core IDrawFile
	*/
	static inline void			removeOne		( IDrawFile * new_it )
	{ Q_ASSERT( uniq_->lst_.contains( new_it ) );
		int i = uniq_->lst_.indexOf( new_it );
		if ( i >= PROTECTED_IDRAWFILE_CONTENT )
			remove( i );
	}

protected:


	/**
	*	@brief	add an item at the end of the list
	*/
	static void					append			( IDrawFile * new_it );


	/**
	*	@brief	remove an item from the list
	*
	*	Will not remove core IDrawFile
	*/
	static void					remove			( int i );


	///@}
	/* ******************************************************************* */




	/*  FUNCTIONS    ======================================================= */
	//
	//
	//
	//

};	/*	class FormatManager	*/

/*  CLASS    =============================================================== */
//
//
//
//

}	//	namespace	cpg

#endif // __FORMATMANAGER_INC__
/* ------------------------------------------------------------------------- */
/* ========================================================================= */
